###################################################################################################
## Payson (2021) When Cities Lobby Replication Code
## Chapter Two: Figures (2.1 - 2.5) and Tables (2.1 - 2.2)
## Created with R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
###################################################################################################


## Load packages
library(maps); library(ggplot2); library(scales); library(reshape)
library(RColorBrewer); library(tidyverse)


## Set working directory
setwd()


###################################################################################################
## Figures 2.1 - 2.5
###################################################################################################


###################################################################################################
## Figure 2.1 Lobbying Expenditures in California
###################################################################################################

dta <- read.csv("ca_lobbying_timeseries.csv")

dta$Sector <- factor(dta$Sector, 
                     levels = c("Local Government", "Health Care", "Manufacturing", 
                                "Finance", "Oil and Gas", "Education", "Labor Unions", 
                                "Professional/ Trade", "Utilities", "Transportation", 
                                "Entertainment", "Real Estate", "Retail", "Agriculture", 
                                "Legal", "Public Employees", "Lodging/ Restaurants", 
                                "Political Organizations", "Miscellaneous"))

dta <- na.omit(subset(dta, Sector == "Local Government" | Sector == "Health Care" | 
                        Sector == "Manufacturing" | Sector == "Finance" | 
                        Sector == "Oil and Gas" | Sector == "Education" | 
                        Sector == "Labor Unions" | Sector == "Professional/ Trade"))


(p <- ggplot(dta, aes(x=year, y=dollars_2013, linetype=Sector, colour=Sector)) +
    geom_line(size = 1) + coord_cartesian(xlim=c(1999,2015)) +
    scale_linetype_manual(name = "Interest Sector", 
                          values = c("solid", "dotted", 
                                     "twodash", "solid", "dotted", 
                                     "twodash", "solid", "dotted")) +
    xlab("Legislative Session") + ylab("2015 Dollars") +
    scale_x_continuous(breaks=seq(1999, 2015, 3)) + scale_y_continuous(labels=comma) +
    scale_colour_manual(name = "Interest Sector", 
                        values = c("gray1", "gray20", "gray30", "gray40", 
                                   "gray50", "gray60", "gray70", "gray80")) +
    theme_bw(base_size = 12, base_family = "serif") +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          strip.background = element_rect(fill = "white")))



###################################################################################################
## Figure 2.2 Total Local Government Lobbying by Type, 2006–2015
###################################################################################################

dta <- read.csv("govt_lobby.csv")


plot.dta <- data.frame(amount = table(dta$type))
plot.dta$amount.Var1 <- factor(plot.dta$amount.Var1, 
                               levels = c("city", "county", "school", "special"),
                               labels = c("Cities", "Counties", 
                                          "School Districts", "Special Districts"))


(p <- ggplot(plot.dta, aes(x = amount.Var1, y = amount.Freq)) +
    geom_bar(stat = "identity", alpha = .8, width = .5) +
    labs(y = "Frequency", x = "Government Type") +
    scale_y_continuous(labels=comma) +
    theme_bw(base_size = 12, base_family = "serif") +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()))



###################################################################################################
## Figure 2.3: Cities Lobbying the State and Federal Government
###################################################################################################

dta <- read.csv("city_lobby.csv")

plot.dta <- dta %>%
  group_by(year) %>%
  summarize(state = sum(lobby),
            fed = sum(fed.lobby))

plot.dta <- as.data.frame(plot.dta)

plot.dta <- melt(plot.dta, id = "year")

(p <- ggplot(plot.dta, aes(x=year, y = value, linetype =variable)) +
    geom_point() + ylim(250, 1000) +
    labs(x = "Year", y = "Number of Cities") +
    scale_linetype(name = "", labels = c("Lobbying State Government", 
                                         "Lobbying Federal Government")) +
    theme_bw(base_size = 12, base_family = "serif") +
    scale_x_continuous(limits=c(2006, 2015), breaks = seq(2006, 2015, 2)) +
    geom_line() + theme(plot.background = element_blank(),
                        legend.position = "top",
                        panel.grid.major = element_blank(),
                        panel.grid.minor = element_blank()))



###################################################################################################
## Figure 2.4 Geographic Distribution of Lobbying Cities, Population 20,000+.
###################################################################################################

dta <- read.csv("city_lobby.csv")


## Subet to cities with population over 20,000 and fix to a single year
tmp <- dta[dta$year==2006 & dta$pop > 20000, ]


## Load map data
states_map <- map_data("state")


plot.dta <- data.frame(x=tmp$long, y = tmp$lat,
                        lobby = factor(tmp$ever.lobby), state = tmp$state)
plot.dta <- subset(plot.dta, y < 50)

plot.dta$state <- tolower(plot.dta$state)

(p <- ggplot(plot.dta, aes(x=x, y=y, map_id = state)) +
    geom_map(map = states_map, colour = "black", fill = "white") +
    geom_point(alpha = .8, size = .8, aes(colour= lobby)) +
    scale_colour_manual(name = "", labels  =  c("Non-Lobbying City", "Lobbying City"),
                        values = c("gray80","black")) +
    theme_minimal(base_family = "serif", base_size = 13) +  
    theme(plot.background = element_blank(),
          legend.position = "top",
          axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks.x = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()))



###################################################################################################
## Figure 2.5 Figure 2.5 Legislative vs. Executive Branch Lobbying in Florida
###################################################################################################

dta <- read.csv("fl_leg_exec.csv")

dta$Branch <- factor(dta$Branch, levels = c("Executive", "Legislative"))

(p <- ggplot(dta, aes(x=Year, y=Number, linetype=Branch)) + 
  geom_point() + 
  geom_line() +
  xlab("Year") + 
  ylab("Number of Cities Lobbying") +
  ylim(50, 150) +
  scale_linetype(breaks = c("Legislative", "Executive"), name = "") +
  scale_x_continuous(limits=c(2007, 2015), breaks = seq(2007, 2015, 2)) +
  theme_bw(base_size = 12, base_family = "serif") +
  theme(plot.background = element_blank(),
        legend.position = "top",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "white")))



###################################################################################################
## Tables 2.1 and 2.2
###################################################################################################


###################################################################################################
## Table 2.1 Cities Lobbying State vs. Federal Government 
###################################################################################################

dta <- read.csv("city_lobby.csv")

## Subset to years of complete data
dta <- dta[dta$year>2005, ]

## Define city lobbying categories (neither, state only, fed only, or both)
dta$cat <- ifelse(dta$lobby == 0 & dta$fed.lobby == 0, "neither",
                  ifelse(dta$lobby == 1 & dta$fed.lobby == 0, "state only",
                         ifelse(dta$lobby == 0 & dta$fed.lobby == 1, "fed only", 
                                "both")))

dta$cat <- factor(dta$cat, levels = c("state only", "both", "fed only", NA),
                  labels = c("State Only", "State and Federal", "Federal Only"))

print(tab <- cbind(N = table(dta$cat), Pct = round(prop.table(table(dta$cat))*100)))



###################################################################################################
## Table 2.2 City Lobbying by Population
###################################################################################################

dta <- read.csv("city_lobby.csv")

## Subset to years of complete data
dta <- dta[dta$year>2005, ]

dta$pop.cat <- cut(dta$mean.pop, breaks = c(5000, 20000, 50000, 100000, 500000, 10000000),
                   include.lowest = TRUE, labels = c("5,000 - 20,000", "20,001 - 50,000",
                                                     "50,001 - 100,000", "100,001 - 500,000", 
                                                     "Over 500,000"))


n <- table(dta$pop.cat[dta$year==2015])
prop.state <- tapply(dta$lobby, dta$pop.cat, mean)
prop.fed <- tapply(dta$fed.lobby, dta$pop.cat, mean)

print(tab <- data.frame("Population" = rownames(n),
                        "N" = as.character(format(n, big.mark = ",")), 
                        "State" = round(prop.state*100), 
                        "Federal" = round(prop.fed*100),
                        row.names = NULL))

